package com.lbmb.mdps.settings.service.impl;

import com.lbmb.mdps.commons.domain.ChartVo;
import com.lbmb.mdps.commons.exception.EmployeeException;
import com.lbmb.mdps.commons.exception.MedicineException;
import com.lbmb.mdps.commons.exception.MedicineTypeException;
import com.lbmb.mdps.settings.dao.EmployeeDao;
import com.lbmb.mdps.settings.domain.Employee;
import com.lbmb.mdps.settings.service.ChartService;
import com.lbmb.mdps.settings.service.EmployeeService;
import com.lbmb.mdps.workbench.dao.DeliverDao;
import com.lbmb.mdps.workbench.dao.MedicineDao;
import com.lbmb.mdps.workbench.dao.MedicineTypeDao;
import com.lbmb.mdps.workbench.dao.PurchaseDao;
import com.lbmb.mdps.workbench.domain.MedicineType;
import com.lbmb.mdps.workbench.service.MedicineTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-06-02 13:04
 * description
 */
@Service("chartService")
public class ChartServiceImpl implements ChartService {

    @Resource
    private MedicineTypeDao medicineTypeDao;
    @Resource
    private MedicineDao medicineDao;
    @Resource
    private EmployeeDao employeeDao;
    @Resource
    private PurchaseDao purchaseDao;
    @Resource
    private DeliverDao deliverDao;

    @Override
    public List<ChartVo> initMedicineTypePieChart() throws MedicineTypeException, MedicineException {
        /*
         * 获得药品类别饼状图数据
         * @author 林文塨
         * @date 2022/6/2 13:07
         * @param []
         * @return java.util.List<com.lbmb.mdps.commons.domain.ChartVo>
         */

        List<ChartVo> chartVoList = new ArrayList<>();

        List<MedicineType> medicineTypeList = medicineTypeDao.getAllMedicineType();

        if (medicineTypeList == null) {
            throw new MedicineTypeException("获得全部药品类别失败！");
        }

        for (MedicineType medicineType : medicineTypeList) {
            ChartVo chartVo = new ChartVo();

            int medicineNumber = medicineDao.getMedicineNumberByMedicineType(medicineType.getId());

            if (medicineNumber < 0) {
                throw new MedicineException("获取药品数量失败！");
            }

            chartVo.setName(medicineType.getType());
            chartVo.setValue(medicineNumber);

            chartVoList.add(chartVo);
        }


        return chartVoList;
    }

    @Override
    public List<ChartVo> initMedicineAttributePieChart() {
        /*
         * 获得药品属性饼状图数据
         * @author 林文塨
         * @date 2022/6/2 13:24
         * @param []
         * @return java.util.List<com.lbmb.mdps.commons.domain.ChartVo>
         */
        List<ChartVo> chartVoList = new ArrayList<>();

        /*来不及了，用蠢方法写了*/
        ChartVo chartVo1 = new ChartVo();
        String medicalInsurance = "医保";
        int medicalInsuranceNumber = medicineDao.getMedicineNumberByMedicalInsurance(medicalInsurance);
        chartVo1.setName(medicalInsurance);
        chartVo1.setValue(medicalInsuranceNumber);
        chartVoList.add(chartVo1);

        ChartVo chartVo2 = new ChartVo();
        String disMedicalInsurance = "非医保";
        int disMedicalInsuranceNumber = medicineDao.getMedicineNumberByMedicalInsurance(disMedicalInsurance);
        chartVo2.setName(disMedicalInsurance);
        chartVo2.setValue(disMedicalInsuranceNumber);
        chartVoList.add(chartVo2);

        ChartVo chartVo3 = new ChartVo();
        String donate = "捐赠";
        int donateNumber = medicineDao.getMedicineNumberByDonate(donate);
        chartVo3.setName(donate);
        chartVo3.setValue(donateNumber);
        chartVoList.add(chartVo3);

        ChartVo chartVo4 = new ChartVo();
        String disDonate = "非捐赠";
        int disDonateNumber = medicineDao.getMedicineNumberByDonate(donate);
        chartVo4.setName(disDonate);
        chartVo4.setValue(disDonateNumber);
        chartVoList.add(chartVo4);

        return chartVoList;
    }

    @Override
    public List<ChartVo> initEmployeePurchasePieChart() throws EmployeeException {
        /*
         * 获得员工采购饼状图数据
         * @author 林文塨
         * @date 2022/6/2 13:41
         * @param []
         * @return java.util.List<com.lbmb.mdps.commons.domain.ChartVo>
         */

        List<ChartVo> chartVoList = new ArrayList<>();

        List<Employee> employeeList = employeeDao.getAllEmployee();

        if (employeeList == null) {
            throw new EmployeeException("获得全部员工失败！");
        }

        for (Employee employee : employeeList) {
            ChartVo chartVo = new ChartVo();

            int purchaseNumber = purchaseDao.getPurchaseNumberByEmployeeId(employee.getId());
            chartVo.setValue(purchaseNumber);
            chartVo.setName(employee.getName());

            chartVoList.add(chartVo);
        }

        return chartVoList;
    }

    @Override
    public List<ChartVo> initEmployeeDeliverPieChart() throws EmployeeException {
        /*
         * 获得员工出库饼状图数据
         * @author 林文塨
         * @date 2022/6/2 13:41
         * @param []
         * @return java.util.List<com.lbmb.mdps.commons.domain.ChartVo>
         */

        List<ChartVo> chartVoList = new ArrayList<>();

        List<Employee> employeeList = employeeDao.getAllEmployee();

        if (employeeList == null) {
            throw new EmployeeException("获得全部员工失败！");
        }

        for (Employee employee : employeeList) {
            ChartVo chartVo = new ChartVo();

            int deliverNumber = deliverDao.getPurchaseNumberByEmployeeId(employee.getId());
            chartVo.setValue(deliverNumber);
            chartVo.setName(employee.getName());

            chartVoList.add(chartVo);
        }

        return chartVoList;
    }
}
